<template>
    <div>
        {{event_message}}
    </div>
</template>

<script>
    export default {
        name: "EventDemo",
        data() {
            return {
                event_message: '等待事件中...'
            }
        },
        methods: {
            //设置事件监听器
            listenEvent(eventName) {
                this.$on(eventName, function (message) {
                    this.event_message = message
                })
            },

            //触发事件
            triggerEvent(eventName, message) {
                setTimeout(() => {
                    this.$emit(eventName, message)
                }, 1000)
            }
        },
        created() {
            this.listenEvent('event1')
            console.log('设置了事件监听器1')
            this.triggerEvent('event1', '触发了事件1')
        }
    }
</script>

<style scoped>

</style>
